package com.graduation.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.springbootinit.mapper.ProductImageMapper;
import com.graduation.springbootinit.model.entity.ShopPart.ProductImage;
import com.graduation.springbootinit.service.ProductImageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductImageServiceImpl extends ServiceImpl<ProductImageMapper, ProductImage>
        implements ProductImageService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveProductImages(Long productId, List<String> imageNames) {
        List<ProductImage> productImages = new ArrayList<>();
        for (String imageName : imageNames) {
            ProductImage productImage = new ProductImage();
            productImage.setProductId(productId);
            productImage.setImageName(imageName);
            productImages.add(productImage);
        }
        saveBatch(productImages);
    }

    @Override
    public void saveProductImage(Long productId, String imageName) {
        ProductImage productImage = new ProductImage();
        productImage.setProductId(productId);
        productImage.setImageName(imageName);
        save(productImage);
    }

    @Override
    public void deleteProductImage(Long id) {
        removeById(id);
    }

    @Override
    public void deleteByProductId(Long productId) {
        QueryWrapper<ProductImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        remove(queryWrapper);
    }

    @Override
    public List<ProductImage> getImagesByProductId(Long productId) {
        QueryWrapper<ProductImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);
        return list(queryWrapper);
    }
} 